[PR]

水無瀬の部屋 > Programming > sample > exe2ico > rsrc2cur.cpp
最終更新日: 2007/03/24

   1: //*********************************************************
   2: // プロジェクト: exe2ico
   3: //  ファイル名: rsrc2cur.cpp
   4: //*********************************************************
   5: #include "rsrc2cur.h"
   6: #include "util.h"
   7: #include <filefmt/curfile.h>
   8: 
   9: 
  10: //---------------------------------------------------------
  11: // ファイルスコープ関数 の 宣言
  12: //---------------------------------------------------------
  13: static size_t put_cfh(     BYTE *buf, size_t bufsize, size_t offset, const CURSORFILEHEAD *cfh );
  14: static size_t put_cursors( BYTE *buf, size_t bufsize, size_t offset, const CURSORRESINF *cri, int num, HMODULE hModule );
  15: static size_t put_cursor(  BYTE *buf, size_t bufsize, size_t cih_offset, size_t img_offset, const CURSORRESINF *cri, HMODULE hModule );
  16: 
  17: 
  18: //*********************************************************
  19: // rsrc2cur
  20: //*********************************************************
  21: size_t
  22: CALLBACK
  23: rsrc2cur
  24: 	(
  25: 		      BYTE    *buf,
  26: 		      size_t   bufsize,
  27: 		      HMODULE  hModule,
  28: 		const char    *name,
  29: 		const char    *type
  30: 	)
  31: {
  32: 	// パラメタの仮定
  33: 	ASSERT( IsValidInstanceHandle( hModule ) );
  34: 	ASSERT( IsValidResourceName( name ) );
  35: 	ASSERT( IsValidResourceName( type ) );
  36: 	ASSERT(  buf || (0 == bufsize) );
  37: 	ASSERT( !buf || IsValidPtr( buf, bufsize ) );
  38: 	ASSERT( !buf || (DESTROY_BUFFER( buf, bufsize ), true) ); // [破壊]
  39: 
  40: 	//
  41: 	size_t size = 0;
  42: 
  43: 	//
  44: 	const void *src = (BYTE *)GetResourcePtr( hModule, name, type );
  45: 	if ( src )
  46: 	{
  47: 		// 
  48: 		const CURSORFILEHEAD *cfh = static_cast<const CURSORFILEHEAD *>( src );
  49: 		size += put_cfh( buf, bufsize, size, cfh++ );
  50: 
  51: 		//
  52: 		const CURSORRESINF *cri = static_cast<const CURSORRESINF *>( static_cast<const void *>( cfh ) );
  53: 		size += put_cursors( buf, bufsize, size, cri, cfh->num, hModule );
  54: 	}
  55: 
  56: 	ASSERT( !buf || (size <= bufsize) );
  57: 	ASSERT( !buf
  58: 		|| (size    == rsrc2cur( null, 0, hModule, name, type ))
  59: 		|| (bufsize <= rsrc2cur( null, 0, hModule, name, type )) );
  60: 	return size;
  61: }//rsrc2cur
  62: 
  63: 
  64: //******************************************************************************************************************
  65: // private
  66: //******************************************************************************************************************
  67: //*********************************************************
  68: // put_cfh
  69: //*********************************************************
  70: static
  71: size_t
  72: put_cfh
  73: 	(
  74: 		      BYTE           *buf,
  75: 		      size_t          bufsize,
  76: 		      size_t          offset,
  77: 		const CURSORFILEHEAD *cfh
  78: 	)
  79: {
  80: 	// パラメタの仮定
  81: 	ASSERT(  buf || (0 == bufsize) );
  82: 	ASSERT( !buf || (offset < bufsize) );
  83: 	ASSERT( !buf || IsValidPtr( buf, bufsize ) );
  84: 	ASSERT( IsValidReadPtr( cfh, sizeof( *cfh ) ) );
  85: 
  86: 	//
  87: 	const size_t size = sizeof( *cfh );
  88: 
  89: 	//
  90: 	if ( buf )
  91: 	{
  92: 		memmove( buf + offset, cfh, size );
  93: 	}
  94: 
  95: 	ASSERT( !buf || (offset + size <= bufsize) );
  96: 	ASSERT( !buf
  97: 		|| (size    ==          put_cfh( null, 0, offset, cfh ))
  98: 		|| (bufsize <= offset + put_cfh( null, 0, offset, cfh )) );
  99: 	return size;
 100: }//put_cfh
 101: 
 102: //*********************************************************
 103: // put_cursors
 104: //*********************************************************
 105: static
 106: size_t
 107: put_cursors
 108: 	(
 109: 		      BYTE         *buf,
 110: 		      size_t        bufsize,
 111: 		      size_t        offset,
 112: 		const CURSORRESINF *cri,
 113: 		      int           num,
 114: 		      HMODULE       hModule
 115: 	)
 116: {
 117: 	// パラメタの仮定
 118: 	ASSERT( 0 < num );
 119: 	ASSERT( IsValidReadPtr( cri, num * sizeof( *cri ) ) );
 120: 	ASSERT( IsValidInstanceHandle( hModule ) );
 121: 	ASSERT(  buf || (0 == bufsize) );
 122: 	ASSERT( !buf || (offset < bufsize) );
 123: 	ASSERT( !buf || IsValidPtr( buf, bufsize ) );
 124: 
 125: 	//
 126: 	size_t size = (num * sizeof( CURSORINFOHEAD ));
 127: 
 128: 	//
 129: 	{for( int i = 0; i < num; ++i )
 130: 	{
 131: 		size += put_cursor( buf, bufsize, offset + (i * sizeof( CURSORINFOHEAD )), offset + size, &cri[ i ], hModule );
 132: 	}}
 133: 
 134: 	ASSERT( !buf || (offset + size <= bufsize) );
 135: 	ASSERT( !buf
 136: 		|| (size    ==          put_cursors( null, 0, offset, cri, num, hModule ))
 137: 		|| (bufsize <= offset + put_cursors( null, 0, offset, cri, num, hModule )) );
 138: 	return size;
 139: }//put_cursors
 140: 
 141: //*********************************************************
 142: // put_cursor
 143: //*********************************************************
 144: static
 145: size_t
 146: put_cursor
 147: 	(
 148: 		      BYTE         *buf,
 149: 		      size_t        bufsize,
 150: 		      size_t        cih_offset,
 151: 		      size_t        img_offset,
 152: 		const CURSORRESINF *cri,
 153: 		      HMODULE       hModule
 154: 	)
 155: {
 156: 	// パラメタの仮定
 157: 	ASSERT( cih_offset < img_offset );
 158: 	ASSERT( IsValidReadPtr( cri, sizeof( *cri ) ) );
 159: 	ASSERT( IsValidInstanceHandle( hModule ) );
 160: 	ASSERT(  buf || (0 == bufsize) );
 161: 	ASSERT( !buf || (cih_offset < bufsize) );
 162: 	ASSERT( !buf || (img_offset < bufsize) );
 163: 	ASSERT( !buf || IsValidPtr( buf, bufsize ) );
 164: 	
 165: 	// cri->size がホットスポットの分を含むためか?
 166: 	const size_t imgsize = cri->size - (2 * sizeof( WORD ));
 167: 
 168: 	//
 169: 	const void *rsrc = (BYTE *)GetResourcePtr( hModule, MAKEINTRESOURCE( cri->wID ), RT_CURSOR );
 170: 	if ( rsrc )
 171: 	{
 172: 		const WORD *wHotSpot = static_cast<const WORD *>( rsrc );
 173: 		const BYTE *img = static_cast<const BYTE *>( static_cast<const void *>( wHotSpot + 2 ) );
 174: 
 175: 		if ( buf )
 176: 		{
 177: 			const size_t size = sizeof(BITMAPINFOHEADER) + (2 * sizeof(RGBQUAD))
 178: 				+ ((cri->height / 2) * ((cri->width / 8) + 3 & ~3))
 179: 				+ ((cri->height / 2) * ((cri->width / 8) + 3 & ~3));
 180: 
 181: 			//
 182: 			CURSORINFOHEAD *cih = static_cast<CURSORINFOHEAD *>( static_cast<void *>( buf + cih_offset ) );
 183: 			cih->size      = _w64_static_cast<DWORD>( imgsize ); // _W64
 184: 			cih->width     = static_cast<BYTE>( cri->width );
 185: 			cih->height    = static_cast<BYTE>( (size == cih->size) ? (cri->height / 2) : cri->height );
 186: 			cih->color     = 0;
 187: 			cih->unknown   = 0;
 188: 			cih->wHotSpotX = wHotSpot[ 0 ];
 189: 			cih->wHotSpotY = wHotSpot[ 1 ];
 190: 			cih->offset    = _w64_static_cast<DWORD>( img_offset ); // _W64
 191: 		}
 192: 
 193: 		if ( buf )
 194: 		{
 195: 			memmove( buf + img_offset, img, imgsize );
 196: 		}
 197: 	}
 198: 
 199: 	ASSERT( !buf || (img_offset + imgsize <= bufsize) );
 200: 	ASSERT( !buf
 201: 		|| (imgsize ==              put_cursor( null, 0, cih_offset, img_offset, cri, hModule ))
 202: 		|| (bufsize <= img_offset + put_cursor( null, 0, cih_offset, img_offset, cri, hModule )) );
 203: 	return imgsize;
 204: }//put_cursor
 205: 
 206: 
 207: //** end **

参照:


Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬の部屋 > sample > exe2ico > rsrc2cur.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/exe2ico/rsrc2cur_cpp.shtml
>> Amazon.co.jp 『たまゆら童子』 へ
>> 楽天ブックス 『たまゆら童子』 へ